/*
 * Creates connection with the clients
 * Licensed by the Bunie Developmentgroup.
 */
package infoserver;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * This class is responsible for the connection with the client. It creates a
 * new socket, accepts a connection and listen for incoming messages.
 */

/**
 * @author Wouter Brunekreeft
 * @since 20-04-2012
 * @version 0.9
 */
public class Server {

    private int portnumber;
    private ServerSocket listenPort = null;
    private final static Logger LOGGER = Logger.getLogger(Log.class.getName());

    /**
     * Starts up the server and creates connection.
     *
     * @param portnumber
     * @since v0.1
     */
    
    public Server(int portnumber) {
        this.portnumber = portnumber;
        Log.print("Server is running", Log.INFO);
        this.listenPort = createSocketAndListen();
        Log.print("Server has created port", Log.INFO);
        SocketFactory sf = new SocketFactory(listenPort);

        while (true) {
            
        }
    }

    /**
     * Creates a new ServerSocket and returns the ListenPort
     *
     * @author: Wouter Brunekreeft
     * @return ServerSocket
     * @since v0.1
     */

    private ServerSocket createSocketAndListen() {
        try {
            listenPort = new ServerSocket(portnumber);
        } catch (BindException ex) {
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listenPort;
    }

    /**
     * Finalizing: closes the socket.
     */
    @Override
    protected void finalize() {
        finalize();
        try {
            listenPort.close();
        } catch (IOException e) {
            Log.print("Could not close input- /outputstream", Log.SEVERE);
        }
    }
}